import html2canvas from 'html2canvas';
import { jsPDF } from "jspdf";

export const toPDF = async (elName: string, title: string = "导出pdf") => {
	let pdfDom = document.querySelector(elName) as HTMLElement
	pdfDom.style.padding = '0 10px !important'
	const A4Width = 595.28;
	const A4Height = 841.89;
	let canvas = await html2canvas(pdfDom, {
		scale: 2,
		useCORS: true,
		backgroundColor: '#ffffff',
	});
	let pageHeight = (canvas.width / A4Width) * A4Height;
	let leftHeight = canvas.height;
	let position = 0;
	let imgWidth = A4Width;
	let imgHeight = (A4Width / canvas.width) * canvas.height;

	let pageData = canvas.toDataURL("image/jpeg", 1.0);
	let PDF = new jsPDF("p", 'pt', 'a4');
	if (leftHeight < pageHeight) {
		PDF.addImage(pageData, "JPEG", 0, 0, imgWidth, imgHeight);
	} else {
		while (leftHeight > 0) {
			PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
			leftHeight -= pageHeight;
			position -= A4Height;
			if (leftHeight > 0) PDF.addPage();
		}
	}
	PDF.save(title + ".pdf");
}
